import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import {
  commonDetailPayload,
  getCompanyID,
  getPosIDs,
  getTenantID,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 添加委托
 * @param callback 接口回调
 * @returns
 */
const addDelegateService = (callback: (res: any, clickType: boolean) => void) => {
  const addDelegate = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/delegateController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    addDelegateRun: addDelegate?.run,
    addDelegateLoading: addDelegate?.loading,
  };
};

/**
 * 编辑委托
 * @param callback 接口回调
 * @returns
 */
const editDelegateService = (callback: (res: any) => void) => {
  const editDelegate = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/delegateController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    editDelegateRun: editDelegate?.run,
    editDelegateLoading: editDelegate?.loading,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getDelegateByIdService = (callback: (res: any) => void) => {
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}`, //流程类别
  ];

  const getDelegateById = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/delegateController/getMactivitiDelegateByID`;
      const payload: Record<string, any> = {
        ...params,
        dicTypeIds: loadMdicIds?.toString(),
        dicTransName: 'category',
        orgTransName: `${ISearchTranslateAll.DELEGATE_DEPT},${ISearchTranslateAll.AGENT_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.MODIFY_DEPT}`,
      };
      return requestFunComponFiex('PUT', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getDelegateByIdRun: getDelegateById?.run,
    getDelegateByIdLoading: getDelegateById?.loading,
    getDelegateByIdInfo: getDelegateById?.data?.obj,
  };
};

/**
 * 设置状态
 * @param callback 接口回调
 * @returns
 */
const setDelegateSwitchService = (callback: (res: any) => void) => {
  const setDelegateSwitch = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/delegateController/setSwitch`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    setDelegateSwitchRun: setDelegateSwitch?.run,
    setDelegateSwitchLoading: setDelegateSwitch?.loading,
  };
};

export { addDelegateService, editDelegateService, getDelegateByIdService, setDelegateSwitchService };
